﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUUserToSkill
    {
        /// <summary>
        /// Get All the UserToSkill of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<UserToSkill> GetAllUserToSkill(bool searchInDeleted)
        {
            List<UserToSkill> UserToSkills = new List<UserToSkill>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from UserToSkill in db.UserToSkill
                              where UserToSkill.uts_isdeleted == searchInDeleted
                              select UserToSkill;
                foreach (UserToSkill tmp in results)
                    UserToSkills.Add(tmp);
            }

            return UserToSkills;
        }

        /// <summary>
        /// Get the UserToSkill by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UserToSkill GetUserToSkillByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var uts = from UserToSkill in db.UserToSkill
                              where UserToSkill.uts_id == id
                              select UserToSkill;
                    foreach (UserToSkill tmp in uts)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// uts a UserToSkill into the database.
        /// </summary>
        /// <param name="uts"></param>
        /// <returns></returns>
        public static UserToSkill UserToSkillrea(UserToSkill uts)
        {
            if (uts != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    UserToSkill nuts = db.UserToSkill.Add(uts);
                    db.SaveChanges();
                    return nuts;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific UserToSkill of the database.
        /// </summary>
        /// <param name="uts"></param>
        /// <returns></returns>
        public static bool RemoveUserToSkill(UserToSkill uts)
        {
            if (uts != null)
            {
                uts.uts_isdeleted = true;
                if (UpdateUserToSkill(uts) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the UserToSkill into the database.
        /// </summary>
        /// <param name="uts"></param>
        /// <returns></returns>
        public static UserToSkill UpdateUserToSkill(UserToSkill uts)
        {
            if (uts != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from UserToSkill in db.UserToSkill
                                  where UserToSkill.uts_id == uts.uts_id
                                  select UserToSkill;

                    foreach (var tmp in current)
                    {
                        tmp.usr_id = uts.usr_id;
                        tmp.skl_id = uts.skl_id;
                        tmp.uts_isdeleted = uts.uts_isdeleted;

                        db.SaveChanges();
                    }

                    uts = current as UserToSkill;
                }
            }

            return uts;
        }
    }
}
